//
//   NetworkTools .swift
//  weibo1
//
//  Created by changhongliang on 2017/7/25.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit
import Alamofire

enum MethodType {
    case get
    case post
}

let BaseUrl = "https://api.weibo.com/"

class NetworkTools {
    class func requestData(_ type : MethodType, URLString : String, parameters : [String : Any]? = nil, finishedCallback :  @escaping (_ dicts: [String: AnyObject]?, _ error: NSError?) -> ()) {
        
        // 1.获取类型
        let method = type == .get ? HTTPMethod.get : HTTPMethod.post
        
        // 2.发送网络请求
        // 2.1 拼接请求url
        var urlString = URLString
        if !(URLString.hasPrefix("http://")||URLString.hasPrefix("https://")) {
            urlString = BaseUrl + urlString
        }
        
        Alamofire.request(urlString, method: method, parameters: parameters).responseJSON { (response) in
            // 所有返回的结果都在response对象中
            if response.result.isFailure
            {
                finishedCallback(nil, response.result.error! as NSError)
                return
            }
            finishedCallback(response.result.value as? [String : AnyObject], nil)
        }
    }
    
    class func upload(URLString : String, parameters : [String : Any]? = nil,text: String, imageArray: [UIImage]?, finishedCallback :  @escaping (_ dicts: [String: AnyObject]?, _ error: NSError?) -> ()){
        if imageArray?.count == 0 {
            Alamofire.request(URLString, method:.post, parameters: parameters).responseJSON { (
                response) in
                // 所有返回的结果都在response对象中
                if response.result.isFailure
                {
                    finishedCallback(nil, response.result.error! as NSError)
                    return
                }
                finishedCallback(response.result.value as? [String : AnyObject], nil)
            }

            
        }else {
            Alamofire.upload(multipartFormData: { (multipartFormData) in
                for image in imageArray!{
                    // 1.将图片转换为二进制
                    let data = UIImagePNGRepresentation(image)!
                    multipartFormData.append(data, withName: "xxx.png", mimeType: "image/png")
                    // 2.设置请求参数
                    if parameters != nil{
                        for (key, value) in parameters!{
                            multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
                        }
                    
                    }
                }
                
            }, to: URLString, encodingCompletion: { (encodingResult) in
                switch encodingResult {
                    case .success(let upload, _, _):
                        upload.responseJSON { response in
                            finishedCallback(response.result.value as? [String : AnyObject], nil)
                        }
                    case .failure(let encodingError):
                        print(encodingError)
                    finishedCallback(nil,encodingError as NSError)
                    }
            })
        }
    }
    
    
}

